package com.payment.ktb.activity.main4;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.AdapterProfitDetail;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.ProfitDetailEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.StringUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/12/5.
 */

public class ProfitDetailResearchActivity extends BaseActivity {


    @BindView(R.id.tv_profitdetailresearch_count)
    TextView tv_profitdetailresearch_count;
    @BindView(R.id.tv_profitdetailresearch_amount)
    TextView tv_profitdetailresearch_amount;
    @BindView(R.id.prlv_profitdetailresearch)
    PullToRefreshListView prlv_profitdetailresearch;
    private int page = 0;
    private List<ProfitDetailEntity> listDatas = new ArrayList<>();
    private AdapterProfitDetail adapter;
    private String ts;
    private String te;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profitdetailresearch);
        initTitle("分润明细搜索");
        ButterKnife.bind(this);

        initData();
    }

    private void initData() {

        ts = getIntent().getStringExtra("ts");
        te = getIntent().getStringExtra("te");



        // 设置下拉刷新加载文本
        prlv_profitdetailresearch.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
        prlv_profitdetailresearch.getLoadingLayoutProxy(true, false).setReleaseLabel("放开刷新...");
        prlv_profitdetailresearch.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
        // 设置上拉加载文本
        prlv_profitdetailresearch.getLoadingLayoutProxy(false, true).setPullLabel("上拉刷新...");
        prlv_profitdetailresearch.getLoadingLayoutProxy(false, true).setReleaseLabel("放开刷新...");
        prlv_profitdetailresearch.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");



        prlv_profitdetailresearch.setMode(PullToRefreshBase.Mode.BOTH);
        prlv_profitdetailresearch.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            //下拉刷新
            pullDownToRefresh();
        }
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            pullUptoLoad();


        }
    });
    pullDownToRefresh();
    }












    private void pullDownToRefresh() {

        progressDialogUtils.showProgress();
        //构造传输参数
        page = 0;
        Map<String, String> map = new HashMap<String,String>();
        map.put("ts",ts);
        map.put("te",te);
        map.put("limit", "20");
        map.put("page", String.valueOf(page));
        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgProfitRecord, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    progressDialogUtils.dismissProgress();
                    prlv_profitdetailresearch.onRefreshComplete();

                    tv_profitdetailresearch_count.setText(jsonObject.optString("allCount"));
                    tv_profitdetailresearch_amount.setText("¥" + StringUtils.handleFen2Yuan(jsonObject.optString("allPrice")));

                    listDatas.clear();

                    //data
                    listDatas = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<ProfitDetailEntity>>() {}.getType());
                    if (listDatas == null || listDatas.isEmpty()) {
                        listDatas = new ArrayList<ProfitDetailEntity>();
                        ToastUtils.showToast("没有数据！");
                    }
                    //adapter
                    adapter = new AdapterProfitDetail(mContext, listDatas);
                    //bind
                    prlv_profitdetailresearch.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }

            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_profitdetailresearch.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }






    private void pullUptoLoad() {

        progressDialogUtils.showProgress();
        //构造传输参数
        page = page + 1;
        Map<String, String> map = new HashMap<String,String>();
        map.put("ts",ts);
        map.put("te",te);
        map.put("limit", "20");
        map.put("page", String.valueOf(page));
        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgProfitRecord, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                try {

                    JSONObject jsonObject = new JSONObject(response);

                    progressDialogUtils.dismissProgress();
                    prlv_profitdetailresearch.onRefreshComplete();
                    //data
                    List<ProfitDetailEntity> profitDeatils = null;
                    profitDeatils = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<ProfitDetailEntity>>() {}.getType());

                    if (profitDeatils != null && !profitDeatils.isEmpty()) {
                        //adapter
                        listDatas.addAll(profitDeatils);
                        adapter.notifyDataSetChanged();
                    }else {
                        ToastUtils.showToast("没有更多数据啦！");
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }

            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_profitdetailresearch.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }



}
